package com.wontube.smr.dao.bo;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.TVShow;
import com.wontube.smr.client.model.vo.EpisodeItems;
import com.wontube.smr.client.model.vo.TVShowItems;
import com.wontube.smr.dao.mapper.TVShowMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * @author yuechao
 * 
 */
public final class AnimeSelectDao {
	private AnimeSelectDao() {
	}

	public static TVShowItems selectRelatedAnime(String showId, PagenateInfo pageInfo) {		
		TVShow show = TVSelectDao.getTVShow(showId);
		String genre = show.getGenre();		
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		return selectAnimesByExample(ex, genre, pageInfo);		
	}
	
	public static TVShowItems selectAnimes(QueryItemParams params, PagenateInfo pageInfo) {		
		QueryExample ex = new QueryExample();		
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME)
				.addCriterion("tv_show.title_initial=", params.getInitial());

		if ("time".equals(params.getSort())) {
			ex.setOrderByClause("tv_show.air_begin_date desc");
		} else {
			ex.setOrderByClause("tv_show.sort_index asc");
		}

		return selectAnimesByExample(ex, params.getGenre(), pageInfo);		
	}
	
	public static TVShowItems searchAnimes(String keyword, PagenateInfo pageInfo) {		
		StringBuilder sb = new StringBuilder();
		keyword = keyword.replaceAll("[']", "''");
		sb.append("(tv_show.name ilike '%" + keyword + "%')");	
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME)
			.addCriterion(sb.toString());			
		
		ex.setOrderByClause("tv_show.sort_index asc");
		return selectAnimesByExample(ex, null, pageInfo);	
	}
	
	public static TVShowItems selectPopularAnimes(PagenateInfo pageInfo) {
		return selectPopularAnimes(null, pageInfo);
	}
	
	public static TVShowItems selectPopularAnimes(String genreName, PagenateInfo pageInfo) {		
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		ex.setOrderByClause("tv_show.sort_index asc");
		return selectAnimesByExample(ex, genreName, pageInfo);		
	}
	
	public static TVShowItems selectSimulcastAnimes(PagenateInfo pageInfo) {
		TVShowItems items = new TVShowItems(pageInfo);
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		ex.setOrderByClause("tvshow_today.sort_index asc");
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
		
		try {
			TVShowMapper mapper = session.getMapper(TVShowMapper.class);
			items.setItems(mapper.selectWithToday(ex, rowBounds));			
		} finally {
			session.close();
		}
		return items;
	}
	
	public static TVShowItems selectRecentAnimes(String genreName, PagenateInfo pageInfo) {		
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		ex.setOrderByClause("recent_show.sort_index asc");
		
		TVShowItems items = selectRecentAnimesByExample(ex, genreName, pageInfo);
		return items;
	}
	
	public static TVShowItems selectTopRatedAnimes(String genreName,
			PagenateInfo pageInfo) {			
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		ex.setOrderByClause("tv_show.score desc");
		return selectAnimesByExample(ex, genreName, pageInfo);				
	}
	
	//====
	public static EpisodeItems searchAnimeEpisodes(String keyword, PagenateInfo pageInfo) {		
		StringBuilder sb = new StringBuilder();
		keyword = keyword.replaceAll("[']", "''");
		sb.append("(tv_show.name ilike '%" + keyword + "%'");
		sb.append(" or tv_episode.episode_name ilike '%" + keyword + "%')");		
		
		QueryExample ex = new QueryExample();		
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME)
			.addCriterion(sb.toString());
		ex.setOrderByClause("tv_show.sort_index asc, tv_episode.part desc");
		
		return selectEpisodesByExample(ex, null, pageInfo);
	}
	
	public static EpisodeItems selectPopularAnimeEpisodes(PagenateInfo pageInfo) {
		return selectPopularAnimeEpisodes(null, pageInfo);
	}
	
	public static EpisodeItems selectPopularAnimeEpisodes(String genreName, PagenateInfo pageInfo) {
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		ex.setOrderByClause("tv_show.sort_index asc, tv_episode.part desc");
		return selectEpisodesByExample(ex, genreName, pageInfo);
	}
	
	public static EpisodeItems selectRecentAnimeEpisodes(String genreName,
			PagenateInfo pageInfo) {
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		ex.setOrderByClause("tv_show.sort_index asc, tv_episode.part desc");
		
		return selectEpisodesByExample(ex, genreName, pageInfo);
	}
	
	public static EpisodeItems selectTopRatedAnimeEpisodes(String genreName,
			PagenateInfo pageInfo) {
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("tv_show.category=", TVShow.CATEGORY_ANIME);
		ex.setOrderByClause("tv_episode.score desc");
		return selectEpisodesByExample(ex, genreName, pageInfo);
	}
	
	private static TVShowItems selectRecentAnimesByExample(QueryExample ex, String genreName, PagenateInfo pageInfo){
		return TVSelectDao.selectRecentShowsByExample(genreName, ex, pageInfo);
	}
	
	private static TVShowItems selectAnimesByExample(QueryExample ex, String genreName, PagenateInfo pageInfo){
		return TVSelectDao.selectTVShowsByExample(genreName, ex, pageInfo);
	}
	
	private static EpisodeItems selectEpisodesByExample(QueryExample ex, String genreName, PagenateInfo pageInfo){
		return TVSelectDao.selectEpisodesByExample(genreName, ex, pageInfo);
	}
}
